package cn.icanci.ladybird.service.util.impl;

import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import cn.icanci.ladybird.pojo.entity.OrderInfo;
import cn.icanci.ladybird.pojo.entity.TicketSeat;
import cn.icanci.ladybird.service.OrderInfoService;
import cn.icanci.ladybird.service.TicketSeatService;
import cn.icanci.ladybird.service.util.ScheduledService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @author: icanci
 * @date: Created in 2020/10/15 8:53
 * @classAction: 定时任务，用来处理订单
 */
@Service
@Slf4j
public class ScheduledServiceImpl implements ScheduledService {

    @Autowired
    private OrderInfoService orderInfoService;
    @Autowired
    private TicketSeatService ticketSeatService;

    /**
     * cron 表达式
     * 秒 分 时 日 月 周几
     * 下面的就是每天上午2点15触发 - 0 15 2 ? * *
     * -
     * 此定时任务的作用
     * - 处理超时未支付的订单，对其进行设置订单状态。释放座位
     */
    @Transactional(rollbackFor = Exception.class)
    @Scheduled(cron = "0 15 2 ? * *")
    @Override
    public void clearOrderAndSeat() {
        log.info("ScheduledServiceImpl.clearOrderAndSeat - 用来处理订单的定时任务执行... 任务已经开始...");
        List<OrderInfo> orderInfos = orderInfoService.queryAllByOrderInfoStatusIsNotPayment();
        // 查询的结果不为空
        if (null != orderInfos && !orderInfos.isEmpty()) {
            // 遍历订单对象
            for (OrderInfo orderInfo : orderInfos) {
                // 判断订单对象不为空
                if (orderInfo != null) {
                    Date createTime = orderInfo.getCreateTime();
                    Date dateNow = new Date();
                    long between = DateUtil.between(createTime, dateNow, DateUnit.MINUTE);
                    // 如果相差时间大于15，就进行设置订单关闭，状态
                    if (between > 15) {
                        // 取出座位 id
                        String seatsId = orderInfo.getSeatsId();
                        // 座位id不为空
                        if (null != seatsId && seatsId.trim().length() > 0) {
                            // 对座位 id 进行拆分
                            String[] splitIds = seatsId.trim().split(",");
                            // 遍历id
                            for (String splitId : splitIds) {
                                // 强转为Long
                                Long aLong = Long.valueOf(splitId);
                                if (null != aLong) {
                                    // 根据id取出座位
                                    TicketSeat ticketSeat = ticketSeatService.queryById(aLong);
                                    // 设置座位的状态
                                    ticketSeat.setTicketState(2);
                                    ticketSeat.setUpdateTime(new Date());
                                    ticketSeatService.update(ticketSeat);
                                }
                            }
                        }
                    }
                    // 设置订单的状态
                    orderInfo.setOrderInfoStatus(2);
                    orderInfo.setUpdateTime(new Date());
                    orderInfoService.update(orderInfo);
                }
            }
        }
        log.info("ScheduledServiceImpl.clearOrderAndSeat - 用来处理订单的定时任务执行... 任务已经结束...");
    }
}
